package com.chb.myWeather;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
/**
 * 输入:
 * 默认的map输入的是一行行的数据， 按照每行数据字母的序号为键(LongWritable)， 行数据为值(Text)
 * 按照默认情况，我们需要对每行需要进行切割
 * 1925-11-23 15:23:33  23c  时间YYYY-MM-dd空格HH:mm:ss制表符温度
 * 我们自定自定义输入的key和value, 按照制表符切割每行数据
 * 左边为key, 右边为value
 * 那么我们不需要进行切割
 * 
 * 
 * 输出:
 * mapper的输出是使用自定key,
 * 因为输出key中包含year,month,T我们所需的数据， 
 */
public class MyMapper extends Mapper<LongWritable, Text, MyKey, DoubleWritable> {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
		try {
			Date date = sdf.parse(key.toString());
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			int year = c.get(Calendar.YEAR);
			int month =c.get(Calendar.MONTH);
			//获取温度
			Double t = Double.parseDouble(value.toString().substring(0, value.toString().length()-1));
			MyKey myKey = new MyKey();
			myKey.setYear(year);
			myKey.setMonth(month);
			myKey.setT(t);
			//输出
			context.write(myKey, new DoubleWritable(t));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}	
